class_name SteeringBehaviorHelper_GetObstacle
extends SteeringBehaviorHelper

#region 属性
## 检测半径
@export var radius:float = 30
@export var resolution_radio:int = 12
var obstacle_closest_point:Array[Vector2] = []
var ray_cast_2ds:Array[RayCast2D] = []
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init(agent:SteeringBehaviorAgent):
	super.init(agent)
func enable():
	super()
	for i in range(resolution_radio):
		var angle = 360.0 / resolution_radio
		var ray_cast_2d = RayCast2D.new()
		ray_cast_2d.target_position = Vector2(radius,0)
		ray_cast_2d.collision_mask = agent.get_obstacle_layer_mask()
		ray_cast_2d.rotate(deg_to_rad(angle) * i)
		ray_cast_2ds.append(ray_cast_2d)
		agent.add_child(ray_cast_2d)
func disable():
	super()
	for ray in ray_cast_2ds:
		ray.queue_free()
	ray_cast_2ds.clear()
func update():
	super()
	obstacle_closest_point.clear()
	for ray_cast_2d in ray_cast_2ds:
		ray_cast_2d.force_raycast_update()
		if ray_cast_2d.is_colliding():
			obstacle_closest_point.append(ray_cast_2d.get_collision_point())
func destroy():
	super()
#endregion
#region 公共方法
func get_closed_point(pos:Vector2,filter_func:Callable = Callable(),count:int = -1) -> Array[Vector2]:
	var obstacle_closest_point_copy = obstacle_closest_point.duplicate()
	if filter_func and filter_func.is_valid():
		obstacle_closest_point_copy = obstacle_closest_point_copy.filter(filter_func)
	obstacle_closest_point_copy.sort_custom(func (a:Vector2,b:Vector2):return (a-pos).length() < (b-pos).length())
	return obstacle_closest_point_copy.slice(0,count if count >= 0 else obstacle_closest_point_copy.size())
#endregion
#region 私有方法
#endregion
#region 生命周期函数
#endregion
